#include "Snapping.h"
#include "Units.h"
#include <osgManipulator/Projector>
#include <QtCore/QSettings>


osg::Vec3 Snapping::snapPosition(const osg::Vec3 &pos)
{
	QSettings settings;
	double s = settings.value("positionSnappingInterval", QVariant(Units::squareSize() / 2.0)).toDouble();
	float x = osg::round( pos.x() / s ) * s;
	float y = osg::round( pos.y() / s ) * s;
	float z = osg::round( pos.z() / s ) * s;
	return osg::Vec3(x, y, z);
}


osg::Vec3 Snapping::projectMouseToGroundPlane(const osg::Vec2& point, osg::Camera* camera)
{
	osg::Plane plane(osg::Vec3(0,0,1), osg::Vec3(0,0,0)); // x-y plane through origo
	osg::ref_ptr<osgManipulator::PlaneProjector> projector = new osgManipulator::PlaneProjector(plane);
	osgManipulator::PointerInfo pi;
	pi.setCamera(camera);
	pi.setMousePosition(point.x(), camera->getViewport()->height() - point.y());
        osg::Vec3d projectedPoint;
	projector->project(pi, projectedPoint);
	return projectedPoint;
}
